package com.bjtech.web.action.management;

import java.math.BigDecimal;

import org.ssi.web.struts.bean.UploadFile;

import com.bjtech.dao.AssignMeterInfoDao;
import com.bjtech.dao.BaseBuildingInfoDao;
import com.bjtech.dao.BaseRoomInfoDao;
import com.bjtech.dao.BaseUnitInfoDao;
import com.bjtech.dao.SinkInfoDao;
import com.bjtech.dao.SysAccessaryDao;
import com.bjtech.domain.AssignMeterInfo;
import com.bjtech.domain.BaseBuildingInfo;
import com.bjtech.domain.BaseRoomInfo;
import com.bjtech.domain.SinkInfo;
import com.bjtech.domain.SysAccessary;
import com.bjtech.domain.query.SinkInfoQuerySpec;
import com.bjtech.web.action.LyheatBaseAction;

public class SinkInfoAction extends LyheatBaseAction {

	private static final long serialVersionUID = 1L;
	private SinkInfoDao sinkInfoDao;
	private AssignMeterInfoDao assignMeterInfoDao;
	private SinkInfoQuerySpec selectBean;
	
	private SysAccessaryDao sysAccessaryDao;
	private SysAccessary accessary;
	
	private Long meter_id;
	private Long room_id;
	private String custom;
	
	private BaseRoomInfoDao baseRoomInfoDao;
	private BaseBuildingInfoDao baseBuildingInfoDao;
	
	private AssignMeterInfo assignMeterInfo;
	
	public void prepare() throws Exception {

	}
	
	public SinkInfoAction() throws Exception{
		model = new SinkInfo();
		selectBean = new SinkInfoQuerySpec();
	}
	
	public void setSinkInfoDao(SinkInfoDao sinkInfoDao) {
		this.sinkInfoDao = sinkInfoDao;
	}

	public String execute(){
		return this.list();
	}
	
	public String list(){
		selectBean.queryParamString();
		list = this.sinkInfoDao.selectEntityPaginatedList(new SinkInfo(),selectBean);
		return "list";
	}
	
	public String add(){
		model = new SinkInfo();
		((SinkInfo)model).setRoom_id(room_id);
		((SinkInfo)model).setCal_power(new BigDecimal(1));
		((SinkInfo)model).setOther_factor(new BigDecimal(1));
		
		BaseRoomInfo room = baseRoomInfoDao.selectEntity(new BaseRoomInfo(), room_id);
		BaseBuildingInfo build = baseBuildingInfoDao.selectEntity(new BaseBuildingInfo(), room.getBuild_id());
		((SinkInfo)model).setBrand(build.getSink_brand());
		((SinkInfo)model).setSink_height(new BigDecimal(build.getSink_height()));
		((SinkInfo)model).setSink_width(new BigDecimal(build.getSink_width()));
		((SinkInfo)model).setSink_length(new BigDecimal(build.getSink_length()));
		((SinkInfo)model).setSink_length(new BigDecimal(build.getSink_length()));
		((SinkInfo)model).setSink_cen_dis(new BigDecimal(build.getSink_cen_dis()));
		((SinkInfo)model).setCal_power(new BigDecimal(build.getCal_power()));
		return "edit";
	}
	
	public String save(){
		SinkInfo entity = (SinkInfo)model;
		BaseRoomInfo room = baseRoomInfoDao.selectEntity(new BaseRoomInfo(), entity.getRoom_id());
		entity.setBuild_id(room.getBuild_id());
		BaseBuildingInfo build = baseBuildingInfoDao.selectEntity(new BaseBuildingInfo(), room.getBuild_id());
		entity.setArea_id(build.getArea_id());
		
		entity.setPower_factor(new BigDecimal(entity.getPiece_num()).multiply(entity.getCal_power()).multiply(entity.getCouple_factor()).multiply(entity.getOther_factor()));
		
		Long link_id = null;
		AssignMeterInfo meter = assignMeterInfoDao.selectEntity(new AssignMeterInfo(), meter_id);
		if(entity.getId()==null){
			sinkInfoDao.insertEntity(entity);
			link_id = entity.getId();
			//将散热器与分配表关联
			meter.setSink_id(link_id);
			assignMeterInfoDao.updateEntity(meter);
		}else{
			sinkInfoDao.updateEntity(entity);
			link_id = entity.getId();
		}
		
		SysAccessary sysAccessary = new SysAccessary();
		
		if(getUploadFileList().size()>0){//说明本次操作有新文件上传，此时就先把旧文件删除，再添加新文件
			this.sysAccessaryDao.deleteAccByLinkId(link_id);
		}
		for (UploadFile uploadFile : getUploadFileList()) {
			sysAccessary = new SysAccessary();
			sysAccessary.setLink_id(link_id);
			sysAccessary.setLink_tab("P_SINK_INFO");
			sysAccessary.setAc_sname(uploadFile.getFileName());
			sysAccessary.setUrl(uploadFile.getFileSavePath());
			this.sysAccessaryDao.insertEntity(sysAccessary);
		}
		custom = "AssignMeterInfo!list.action?room_id="+meter.getRoom_id();
		super.renderJavaScript("window.onload=function(){location.href='AssignMeterInfo!list.action?room_id="+meter.getRoom_id()+"';}");
		return null;
	}
	
	public String edit(){
		model = this.sinkInfoDao.selectEntity(new SinkInfo(),id);
		//查询附件
		accessary = this.sysAccessaryDao.selectAccListByLinkId(id);
		return "edit";
	}

	public String view(){
		//查询附件
		accessary = this.sysAccessaryDao.selectAccListByLinkId(id);
		model = this.sinkInfoDao.selectEntity(new SinkInfo(),id);
		assignMeterInfo  = this.assignMeterInfoDao.selectAssignMeterInfoBySinkId(id);
		return "view";
	}
	
	public String delete(){
		model = this.sinkInfoDao.deleteEntity(new SinkInfo(),id);
		return "success";
	}
	
	public SinkInfoQuerySpec getSelectBean() {
		return selectBean;
	}

	public void setSelectBean(SinkInfoQuerySpec selectBean) {
		this.selectBean = selectBean;
	}

	public SysAccessaryDao getSysAccessaryDao() {
		return sysAccessaryDao;
	}

	public void setSysAccessaryDao(SysAccessaryDao sysAccessaryDao) {
		this.sysAccessaryDao = sysAccessaryDao;
	}

	public SysAccessary getAccessary() {
		return accessary;
	}

	public void setAccessary(SysAccessary accessary) {
		this.accessary = accessary;
	}

	public SinkInfoDao getSinkInfoDao() {
		return sinkInfoDao;
	}

	public AssignMeterInfoDao getAssignMeterInfoDao() {
		return assignMeterInfoDao;
	}

	public void setAssignMeterInfoDao(AssignMeterInfoDao assignMeterInfoDao) {
		this.assignMeterInfoDao = assignMeterInfoDao;
	}

	public Long getMeter_id() {
		return meter_id;
	}

	public void setMeter_id(Long meterId) {
		meter_id = meterId;
	}

	public String getCustom() {
		return custom;
	}

	public void setCustom(String custom) {
		this.custom = custom;
	}

	public Long getRoom_id() {
		return room_id;
	}

	public void setRoom_id(Long roomId) {
		room_id = roomId;
	}

	public BaseRoomInfoDao getBaseRoomInfoDao() {
		return baseRoomInfoDao;
	}

	public void setBaseRoomInfoDao(BaseRoomInfoDao baseRoomInfoDao) {
		this.baseRoomInfoDao = baseRoomInfoDao;
	}

	public BaseBuildingInfoDao getBaseBuildingInfoDao() {
		return baseBuildingInfoDao;
	}

	public void setBaseBuildingInfoDao(BaseBuildingInfoDao baseBuildingInfoDao) {
		this.baseBuildingInfoDao = baseBuildingInfoDao;
	}

	public AssignMeterInfo getAssignMeterInfo() {
		return assignMeterInfo;
	}

	public void setAssignMeterInfo(AssignMeterInfo assignMeterInfo) {
		this.assignMeterInfo = assignMeterInfo;
	}
}
